var trashRenderObj = {
	getTrashItems : function() {
		var params = 'handler=getTrashItems';
		var ajaxResults = new Ajax.Request('ajaxDirector.php', {
				method: 'POST',
				parameters: params,	
				onSuccess: function(transport) {
					trashRenderObj.renderTrashItems(transport.responseText);
				}
			});
	},
	getNumberItemsInTrash : function(renderTo) {
		var params = 'handler=getNumberItemsInTrash&userid=&renderTo='+renderTo;
		var ajaxResults = new Ajax.Request('ajaxDirector.php', {
				method: 'POST',
				parameters: params,	
				onSuccess: function(transport) {
					var response = transport.responseText;
					var responseArray = response.split('|');
					var itemsInTrash  = responseArray[0];
					var renderTo      = responseArray[1];
					if (renderTo == 'head') {
						trashRenderObj.renderItemsToHeader(itemsInTrash);
					} else if (renderTo == 'title') {
						$('trashTitleItems').innerHTML = itemsInTrash;
					}
				}
			});
	},
	renderItemsToHeader : function (itemsInTrash) {
		if (itemsInTrash>0) {
			$('trashDiv').className = 'trashBin on';
			if (itemsInTrash>1) {
				$('trashDiv').innerHTML = itemsInTrash+' items in trash';
			} else {
				$('trashDiv').innerHTML = itemsInTrash+' item in trash';
			}
			/*
			 * Attach onClick event handler to trashDiv
			 */
			var buttonTrash = Ext.get('trashDiv');
			buttonTrash.on('click', function(){
			    window.location = 'trashBin.php';
			});						
		} else {
			$('trashDiv').className = 'trashBin off';
			$('trashDiv').innerHTML = 'No items in trash';
		}
	},
	renderTrashItems : function(stringJSON) {
		var jsonObject = stringJSON.evalJSON();
		var totalContacts     = jsonObject.Contacts.length;
		var totalEvents       = jsonObject.Events.length;
		var totalMembers      = jsonObject.Members.length;
		var totalFiles        = jsonObject.Files.length;
		var totalLibraryItems = jsonObject.LibraryItems.length;
				
		var stringOut = '';
		
		if (totalContacts>=0) {			
			stringOut += '<h2 style="margin: 5px; width: 50%; float: left; height: 30px;">Contacts</h2>';
			stringOut += '<div style="width: 45%; float: right;" id="messagecontact"></div>';
			stringOut += '<div style="clear: both;"></div>';
			stringOut += '<table class="dataTable" style="font-size: 1em; margin-bottom: 10px;">';
				
			for(a=0;a<=(totalContacts-1);a++) {
				var contactID = jsonObject.Contacts[a].ContactID;
				var contactNo = jsonObject.Contacts[a].IDNumber;
				var subject   = jsonObject.Contacts[a].SubjectLine;
				
				if ((a % 2) > 0) { 
					rowClass = 'altRow'; 
				} else { 
					rowClass = '';
				}			
				
				stringOut += '<tr class="'+rowClass+'" id="rowcontact'+contactID+'">';
				stringOut += '<td style="text-align: center; width: 60px;">';
				stringOut += '<a href="#" onClick="trashUpdateObj.restoreItem(\''+contactID+'\',\'contact\'); return false;" title="Restore this contact"><img src="images/buttonUndoBig.gif" style="border: none;" alt="Restore this contact" /></a> ';
				stringOut += '<a href="#" onClick="trashUpdateObj.confirmDelete(\''+contactID+'\',\'contact\'); return false;" title="Delete this contact"><img src="images/buttonDeleteBig.gif" style="border: none;" alt="Delete" /></a>';
				stringOut += '</td>';
				
				stringOut += '<td style="width: 100px;">ID# '+contactNo+'</td>';
				stringOut += '<td><a href="contactForm.php?contactID='+contactID+'">'+subject+'</a></td></tr>';
				
			}
			stringOut += '</table>';
		}
		if (totalEvents>0) {			
			stringOut += '<h2 style="margin: 5px; width: 50%; float: left; height: 30px;">Events</h2>';
			stringOut += '<div style="width: 45%; float: right;" id="messageevent"></div>';
			stringOut += '<div style="clear: both;"></div>';
			stringOut += '<table class="dataTable" style="font-size: 1em; margin-bottom: 10px;">';
			
			for(a=0;a<=(totalEvents-1);a++) {
				var eventID   = jsonObject.Events[a].EventID;
				var eventName = jsonObject.Events[a].EventName;
				
				if ((a % 2) > 0) { 
					rowClass = 'altRow'; 
				} else { 
					rowClass = '';
				}			
				stringOut += '<tr class="'+rowClass+'" id="rowevent'+eventID+'">';
				stringOut += '<td style="text-align: center; width: 60px;">';
				stringOut += '<a href="#" onClick="trashUpdateObj.restoreItem(\''+eventID+'\',\'event\'); return false;" title="Restore this event"><img src="images/buttonUndoBig.gif" style="border: none;" alt="Restore this event" /></a> ';
				stringOut += '<a href="#" onClick="trashUpdateObj.confirmDelete(\''+eventID+'\',\'event\'); return false;" title="Delete this event"><img src="images/buttonDeleteBig.gif" style="border: none;" alt="Delete" /></a>';
				stringOut += '</td>';				
				stringOut += '<td><a href="#" onClick="calendarUpdateObj.getEventData('+eventID+',\'view\'); return false;">'+eventName+'</a></td></tr>';
			}
			stringOut += '</table>';			
			
		}
		if (totalMembers>0) {		
			stringOut += '<h2 style="margin: 5px; width: 50%; float: left; height: 30px;">Members</h2>';
			stringOut += '<div style="width: 45%; float: right;" id="messagemember"></div>';
			stringOut += '<div style="clear: both;"></div>';
			stringOut += '<table class="dataTable" style="font-size: 1em;">'; 
			
			for(a=0;a<=(totalMembers-1);a++) {
				var memberID  = jsonObject.Members[a].MemberID;
				var firstName = jsonObject.Members[a].FirstName;
				var lastName  = jsonObject.Members[a].LastName;
				var fullName  = firstName+' '+lastName;
				
				if ((a % 2) > 0) { 
					rowClass = 'altRow'; 
				} else { 
					rowClass = '';
				}			
				
				stringOut += '<tr class="'+rowClass+'" id="rowmember'+memberID+'">';
				stringOut += '<td style="text-align: center; width: 60px;">';
				stringOut += '<a href="#" onClick="trashUpdateObj.restoreItem(\''+memberID+'\',\'member\'); return false;" title="Restore this member"><img src="images/buttonUndoBig.gif" style="border: none;" alt="Restore this member" /></a> ';
				stringOut += '<a href="#" onClick="trashUpdateObj.confirmDelete(\''+memberID+'\',\'member\'); return false;" title="Delete this member"><img src="images/buttonDeleteBig.gif" style="border: none;" alt="Delete" /></a>';
				stringOut += '</td>';				
				stringOut += '<td>'+fullName+'</td></tr>';
				
			}
			stringOut += '</table>';
		}		
		if (totalFiles>0) {		
			stringOut += '<h2 style="margin: 5px; width: 50%; float: left; height: 30px;">Files</h2>';
			stringOut += '<div style="width: 45%; float: right;" id="messagefile"></div>';
			stringOut += '<div style="clear: both;"></div>';
			stringOut += '<table class="dataTable" style="font-size: 1em;">'; 
			
			for(a=0;a<=(totalFiles-1);a++) {
				var fileID    = jsonObject.Files[a].FID;
				var fileTitle = jsonObject.Files[a].FileTitle;
				var fileName  = jsonObject.Files[a].FileName;
				
				if ((a % 2) > 0) { 
					rowClass = 'altRow'; 
				} else { 
					rowClass = '';
				}			
				
				stringOut += '<tr class="'+rowClass+'" id="rowfile'+fileID+'">';
				stringOut += '<td style="text-align: center; width: 60px;">';
				stringOut += '<a href="#" onClick="trashUpdateObj.restoreItem(\''+fileID+'\',\'file\'); return false;" title="Restore this file"><img src="images/buttonUndoBig.gif" style="border: none;" alt="Restore this file" /></a> ';
				stringOut += '<a href="#" onClick="trashUpdateObj.confirmDelete(\''+fileID+'\',\'file\'); return false;" title="Delete this file"><img src="images/buttonDeleteBig.gif" style="border: none;" alt="Delete this file" /></a>';
				stringOut += '</td>';				
				stringOut += '<td>'+fileTitle+' ['+fileName+']</td></tr>';
				
			}
			stringOut += '</table>';
		}		
		if (totalLibraryItems>0) {		
			stringOut += '<h2 style="margin: 5px; width: 50%; float: left; height: 30px;">Library Items</h2>';
			stringOut += '<div style="width: 45%; float: right;" id="messagelibraryitem"></div>';
			stringOut += '<div style="clear: both;"></div>';
			stringOut += '<table class="dataTable" style="font-size: 1em;">'; 
			
			for(a=0;a<=(totalLibraryItems-1);a++) {
				var itemID    = jsonObject.LibraryItems[a].LibraryID;
				var itemTitle = jsonObject.LibraryItems[a].Title;
				
				if ((a % 2) > 0) { 
					rowClass = 'altRow'; 
				} else { 
					rowClass = '';
				}			
				
				stringOut += '<tr class="'+rowClass+'" id="rowlibraryitem'+itemID+'">';
				stringOut += '<td style="text-align: center; width: 60px;">';
				stringOut += '<a href="#" onClick="trashUpdateObj.restoreItem(\''+itemID+'\',\'libraryitem\'); return false;" title="Restore this item"><img src="images/buttonUndoBig.gif" style="border: none;" alt="Restore this item" /></a> ';
				stringOut += '<a href="#" onClick="trashUpdateObj.confirmDelete(\''+itemID+'\',\'libraryitem\'); return false;" title="Delete this item"><img src="images/buttonDeleteBig.gif" style="border: none;" alt="Delete this item" /></a>';
				stringOut += '</td>';				
				stringOut += '<td>'+itemTitle+'</td></tr>';
				
			}
			stringOut += '</table>';
		}		
		$('trashItemList').innerHTML = stringOut;
	}
}

var trashUpdateObj = {
	sendToTrash : function(itemID,itemType) {
		var params = 'handler=sendToTrash&itemID='+itemID+'&itemType='+itemType;
		if (itemType == 'memberContact') {
			var ajaxResults = new Ajax.Request('ajaxDirector.php', {
				method: 'POST',
				parameters: params,	
				onSuccess: function(transport) {
					trashRenderObj.getNumberItemsInTrash('head');
				}
			});
		} else if (itemType == 'user') {
			var OK = confirm("Are you sure you want to deactivate this User?");
			if (OK == true) {
				usersObj.deleteUser(itemID);
			}
		} else if (itemType == 'contact') {
			var ajaxResults = new Ajax.Request('ajaxDirector.php', {
				method: 'POST',
				parameters: params,	
				onSuccess: function(transport) {
					trashRenderObj.getNumberItemsInTrash('head');	
					deleteContacts.returnDeleteContact(transport.responseText);
				}
			});
		} else if (itemType == 'event') {
			var ajaxResults = new Ajax.Request('ajaxDirector.php', {
				method: 'POST',
				parameters: params,	
				onSuccess: function(transport) {
					trashRenderObj.getNumberItemsInTrash('head');	
					calendarUpdateObj.returnDeleteEvent(transport.responseText);
				}
			});
		} else if (itemType == 'file') {
			var ajaxResults = new Ajax.Request('ajaxDirector.php', {
				method: 'POST',
				parameters: params,	
				onSuccess: function(transport) {
					trashRenderObj.getNumberItemsInTrash('head');	
					resourcesUpdateObj.returnDeleteFile(transport.responseText);
				}
			});
		} else if (itemType == 'library') {
			var ajaxResults = new Ajax.Request('ajaxDirector.php', {
				method: 'POST',
				parameters: params,	
				onSuccess: function(transport) {
					trashRenderObj.getNumberItemsInTrash('head');	
					libraryUpdateObj.returnDeleteLibraryItem(transport.responseText);
				}
			});
		} 	
	},
	confirmDelete : function(itemID,itemType) {
		var rowEl = $('row'+itemType+itemID);
		Effect.Fade(rowEl, {delay: .1, duration: 1});
		updateStatus('Item deleted','success','message'+itemType);				
		
		if (itemType == 'contact') {
			var handler = 'deleteContact';
		} else if (itemType == 'member') {
			var handler = 'deleteMember';
		} else if (itemType == 'event') {
			var handler = 'deleteEvent';
		} else if (itemType == 'file') {
			var handler = 'deleteFile';
		} else if (itemType == 'libraryitem') {
			var handler = 'deleteLibraryItem';
		}
		
		var params = 'handler='+handler+'&ID='+itemID;
		var ajaxResults = new Ajax.Request('ajaxDirector.php', {
				method: 'POST',
				parameters: params,	
				onSuccess: function(transport) {
					trashRenderObj.getNumberItemsInTrash('head');
					trashRenderObj.getNumberItemsInTrash('title');
				}
			});
	},
	restoreItem : function(itemID,itemType) {
		var rowEl = $('row'+itemType+itemID);
		Effect.Fade(rowEl, {delay: .1, duration: 1});
		updateStatus('Item restored','success','message'+itemType);				
		
		var params = 'handler=restoreTrashItem&itemID='+itemID+'&itemType='+itemType;
		var ajaxResults = new Ajax.Request('ajaxDirector.php', {
				method: 'POST',
				parameters: params,	
				onSuccess: function(transport) {
					trashRenderObj.getNumberItemsInTrash('head');
					trashRenderObj.getNumberItemsInTrash('title');
				}
			});
	}
}